import helper from '../../helper';
import Vue from "vue";

const API_CSRF_COOKIE = '/sanctum/csrf-cookie';
const API_MENU = 'admin/api/my-menu';
const API_DASH_BOARD = 'admin/api/dashboard';
const API_DASH_COUNT = 'admin/api/dash-count';
const API_REAL_TIME_DATA = 'admin/api/real-time-data';
const API_QUICK_SET = 'admin/api/quick-set';
const API_DOWNLOAD_CONFIG = 'admin/api/download-config';

export default {
    namespaced: true,
    state: {
        sidebar: helper.getCache('sidebar', true),
        device: '',
        menu: helper.getCache('menu', []),
        pageOptions: {
            pageSizesList: [10, 15, 20, 30, 50, 100, 200, 500, 1000, 5000],
            layout: "total, sizes, prev, pager, next, jumper"
        },
        pastRangeOptions: {
            firstDayOfWeek: 1,
            shortcuts: [
                {
                    text: '最近三天',
                    onClick(picker) {
                        const start = moment().add(-2, 'd').startOf('d').unix();
                        const end = moment().endOf('d').unix();
                        picker.$emit('pick', [start * 1000, end * 1000]);
                    }
                },
                {
                    text: '最近一周',
                    onClick(picker) {
                        const start = moment().add(-6, 'd').startOf('d').unix();
                        const end = moment().endOf('d').unix();
                        picker.$emit('pick', [start * 1000, end * 1000]);
                    }
                }, {
                    text: '最近一月',
                    onClick(picker) {
                        const start = moment().add(-1, 'M').startOf('d').unix()+86400;
                        const end = moment().endOf('d').unix();
                        picker.$emit('pick', [start * 1000, end * 1000]);
                    }
                }, {
                    text: '最近三个月',
                    onClick(picker) {
                        const start = moment().add(-3, 'M').startOf('d').unix()+86400;
                        const end = moment().endOf('d').unix();
                        picker.$emit('pick', [start * 1000, end * 1000]);
                    }
                }
            ],
            // 禁止选择的时间: 今日24点
            disabledDate (date) {
                return moment(date).unix() < moment().add(-12, 'M').startOf('d').unix() || moment(date).unix() > moment().endOf('d').unix();
            }
        },
        futureRangeOptions: {
            firstDayOfWeek: 1,
            shortcuts: [{
                text: '未来一周',
                onClick(picker) {
                    const end = new Date();
                    const start = new Date();
                    end.setTime(start.getTime() + 3600 * 1000 * 24 * 7);
                    picker.$emit('pick', [start.getTime(), end.getTime()]);
                }
            }, {
                text: '未来一个月',
                onClick(picker) {
                    const end = new Date();
                    const start = new Date();
                    end.setTime(start.getTime() + 3600 * 1000 * 24 * 30);
                    picker.$emit('pick', [start.getTime(), end.getTime()]);
                }
            }, {
                text: '未来三个月',
                onClick(picker) {
                    const end = new Date();
                    const start = new Date();
                    end.setTime(start.getTime() + 3600 * 1000 * 24 * 90);
                    picker.$emit('pick', [start.getTime(), end.getTime()]);
                }
            }],
            // 禁止选择的时间: 当前时间
            disabledDate (date) {
                return moment(date).unix() < moment().startOf('d').unix();
            }
        },
        options: {
            os: {
                0: {label: 'Android', value: 0,},
                1: {label: 'IOS', value: 1,},
            }
        },
        windowInnerHeight: (window.innerHeight > 600 ? window.innerHeight : 600),
        modelUpdated: {},
    },
    mutations: {
        SET_SIDEBAR (state, sidebar) {
            state.sidebar = sidebar;
            helper.setCache('sidebar', sidebar);
        },
        SET_MENU(state, menu) {
            state.menu = menu;
            helper.setCache('menu', menu);
        },
        SET_PAGE_OPTIONS(state) {
            if (helper.isMobile()) {
                state.pageOptions.layout = "prev, pager, next";
            } else {
                state.pageOptions.layout = "total, sizes, prev, pager, next, jumper";
            }
        },
        SET_WINDOW_RESIZE(state) {
            state.windowInnerHeight = window.innerHeight > 600 ? window.innerHeight : 600;
        },
        SET_MODEL_UPDATED(state, data) {
            state.modelUpdated = data;
        }
    },
    actions: {
        InitConfig({commit, state}) {
            commit('SET_PAGE_OPTIONS');
        },
        // action 默认传入context对象
        toggleSidebar ({commit, state}) {
            commit('SET_SIDEBAR', !state.sidebar);
            return true;
        },
        // 初始化
        CsrfCookie ({commit}) {
            return Vue.axios.get(API_CSRF_COOKIE);
        },
        // 菜单
        Menu ({commit}, data) {
            return new Promise((resolve, reject) => {
                Vue.axios.get(API_MENU).then(res => {
                    commit('SET_MENU', res.data.data);
                    return resolve(res);
                }).catch(error => {
                    return reject(error);
                });
            });
        },
        // 面板数据
        DashBoard ({commit}) {
            return Vue.axios.get(API_DASH_BOARD);
        },
        // 快速配置
        DashCount ({commit}, filter) {
            return Vue.axios.get(API_DASH_COUNT,{
                params: filter
            });
        },
        // 实时数据
        RealTimeData ({commit}, data = {}) {
            return Vue.axios.get(API_REAL_TIME_DATA + '/' + data.type, {
                params: data,
            })
        },
        // 快速配置
        QuickSet ({commit}) {
            return Vue.axios.get(API_QUICK_SET);
        },
        // 更新快速配置
        QuickSetUpdate ({commit}, data) {
            return Vue.axios.post(API_QUICK_SET, data);
        },
        // 下载配置
        DownloadConfig({commit}, name) {
            return Vue.axios.get(API_DOWNLOAD_CONFIG +'/'+ name, {
                responseType: 'blob',
            })
        },
    },
    getters: {
        header (state, getters, rootState) {
            return {
                Authorization: 'Bearer ' + rootState.manager.token,
            }
        },
    }
};
